{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5b834328",
   "metadata": {},
   "source": [
    "# reindex重置索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "21538b84",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            A     x         y       C           D\n",
      "0  2016-01-01   0.0  0.164425     Low   86.974078\n",
      "1  2016-01-02   1.0  0.952976    High   96.963019\n",
      "2  2016-01-03   2.0  0.795075     Low  108.433041\n",
      "3  2016-01-04   3.0  0.477981  Medium   87.077419\n",
      "4  2016-01-05   4.0  0.890967     Low   92.125721\n",
      "5  2016-01-06   5.0  0.537113    High  107.295623\n",
      "6  2016-01-07   6.0  0.546628  Medium   98.873128\n",
      "7  2016-01-08   7.0  0.969153    High  109.285315\n",
      "8  2016-01-09   8.0  0.129436     Low   82.614051\n",
      "9  2016-01-10   9.0  0.094826    High   89.427989\n",
      "10 2016-01-11  10.0  0.345978     Low   95.159530\n",
      "11 2016-01-12  11.0  0.684692    High   98.238783\n",
      "12 2016-01-13  12.0  0.412907     Low  103.792796\n",
      "13 2016-01-14  13.0  0.973158    High   82.461932\n",
      "14 2016-01-15  14.0  0.949823    High   97.884558\n",
      "15 2016-01-16  15.0  0.799379    High   95.244324\n",
      "16 2016-01-17  16.0  0.644881     Low  113.582759\n",
      "17 2016-01-18  17.0  0.421216  Medium  100.396482\n",
      "18 2016-01-19  18.0  0.351233     Low   97.470472\n",
      "19 2016-01-20  19.0  0.296730     Low  104.083579\n",
      "           A     C   B\n",
      "0 2016-01-01   Low NaN\n",
      "2 2016-01-03   Low NaN\n",
      "5 2016-01-06  High NaN\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "N=20\n",
    "df = pd.DataFrame({\n",
    "   'A': pd.date_range(start='2016-01-01',periods=N,freq='D'),\n",
    "   'x': np.linspace(0,stop=N-1,num=N),\n",
    "   'y': np.random.rand(N),\n",
    "   'C': np.random.choice(['Low','Medium','High'],N).tolist(),\n",
    "   'D': np.random.normal(100, 10, size=N).tolist()\n",
    "})\n",
    "#重置行、列索引标签\n",
    "df_reindexed = df.reindex(index=[0,2,5], columns=['A', 'C', 'B'])\n",
    "print(df)\n",
    "print(df_reindexed)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "706ddafc",
   "metadata": {},
   "source": [
    "# 遍历\n",
    "如果想要遍历 DataFrame 的每一行，我们下列函数：\n",
    "1) iteritems()：以键值对 (key,value) 的形式遍历；\n",
    "2) iterrows()：以 (row_index,row) 的形式遍历行;\n",
    "3) itertuples()：使用已命名元组的方式对行遍历。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5fc9b3fd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A\n",
      "x\n",
      "y\n",
      "C\n",
      "D\n"
     ]
    }
   ],
   "source": [
    "for col in df:\n",
    "    print(col)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c1314701",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A 0    2016-01-01\n",
      "1    2016-01-02\n",
      "2    2016-01-03\n",
      "3    2016-01-04\n",
      "4    2016-01-05\n",
      "5    2016-01-06\n",
      "6    2016-01-07\n",
      "7    2016-01-08\n",
      "8    2016-01-09\n",
      "9    2016-01-10\n",
      "10   2016-01-11\n",
      "11   2016-01-12\n",
      "12   2016-01-13\n",
      "13   2016-01-14\n",
      "14   2016-01-15\n",
      "15   2016-01-16\n",
      "16   2016-01-17\n",
      "17   2016-01-18\n",
      "18   2016-01-19\n",
      "19   2016-01-20\n",
      "Name: A, dtype: datetime64[ns]\n",
      "x 0      0.0\n",
      "1      1.0\n",
      "2      2.0\n",
      "3      3.0\n",
      "4      4.0\n",
      "5      5.0\n",
      "6      6.0\n",
      "7      7.0\n",
      "8      8.0\n",
      "9      9.0\n",
      "10    10.0\n",
      "11    11.0\n",
      "12    12.0\n",
      "13    13.0\n",
      "14    14.0\n",
      "15    15.0\n",
      "16    16.0\n",
      "17    17.0\n",
      "18    18.0\n",
      "19    19.0\n",
      "Name: x, dtype: float64\n",
      "y 0     0.164425\n",
      "1     0.952976\n",
      "2     0.795075\n",
      "3     0.477981\n",
      "4     0.890967\n",
      "5     0.537113\n",
      "6     0.546628\n",
      "7     0.969153\n",
      "8     0.129436\n",
      "9     0.094826\n",
      "10    0.345978\n",
      "11    0.684692\n",
      "12    0.412907\n",
      "13    0.973158\n",
      "14    0.949823\n",
      "15    0.799379\n",
      "16    0.644881\n",
      "17    0.421216\n",
      "18    0.351233\n",
      "19    0.296730\n",
      "Name: y, dtype: float64\n",
      "C 0        Low\n",
      "1       High\n",
      "2        Low\n",
      "3     Medium\n",
      "4        Low\n",
      "5       High\n",
      "6     Medium\n",
      "7       High\n",
      "8        Low\n",
      "9       High\n",
      "10       Low\n",
      "11      High\n",
      "12       Low\n",
      "13      High\n",
      "14      High\n",
      "15      High\n",
      "16       Low\n",
      "17    Medium\n",
      "18       Low\n",
      "19       Low\n",
      "Name: C, dtype: object\n",
      "D 0      86.974078\n",
      "1      96.963019\n",
      "2     108.433041\n",
      "3      87.077419\n",
      "4      92.125721\n",
      "5     107.295623\n",
      "6      98.873128\n",
      "7     109.285315\n",
      "8      82.614051\n",
      "9      89.427989\n",
      "10     95.159530\n",
      "11     98.238783\n",
      "12    103.792796\n",
      "13     82.461932\n",
      "14     97.884558\n",
      "15     95.244324\n",
      "16    113.582759\n",
      "17    100.396482\n",
      "18     97.470472\n",
      "19    104.083579\n",
      "Name: D, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "for key,value in df.iteritems():\n",
    "   print (key,value)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6af143f0",
   "metadata": {},
   "source": [
    "# 窗口函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "f2c0d999",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             A   B   C   D\n",
      "2020-12-01   0   2   4   6\n",
      "2020-12-02   8  10  12  14\n",
      "2020-12-03  16  18  20  22\n",
      "2020-12-04  24  26  28  30\n",
      "2020-12-05  32  34  36  38\n",
      "               A     B     C     D\n",
      "2020-12-01   NaN   NaN   NaN   NaN\n",
      "2020-12-02   NaN   NaN   NaN   NaN\n",
      "2020-12-03   8.0  10.0  12.0  14.0\n",
      "2020-12-04  16.0  18.0  20.0  22.0\n",
      "2020-12-05  24.0  26.0  28.0  30.0\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "#生成时间序列\n",
    "df = pd.DataFrame(np.arange(0,40,2).reshape(5,4),index = pd.date_range('12/1/2020', periods=5),columns = ['A', 'B', 'C', 'D'])\n",
    "print(df)\n",
    "#每3个数求求一次均值\n",
    "print(df.rolling(window=3).mean())\n",
    "# window=3表示是每一列中依次紧邻的每 3 个数求一次均值。当不满足 3 个数时，所求值均为 NaN 值，因此前两列的值为 NaN，直到第三行值才满足要求 window =3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "720336a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             A   B   C   D\n",
      "2020-12-01   0   2   4   6\n",
      "2020-12-02   8  10  12  14\n",
      "2020-12-03  16  18  20  22\n",
      "2020-12-04  24  26  28  30\n",
      "2020-12-05  32  34  36  38\n",
      "2020-12-06  40  42  44  46\n",
      "2020-12-07  48  50  52  54\n",
      "2020-12-08  56  58  60  62\n",
      "2020-12-09  64  66  68  70\n",
      "2020-12-10  72  74  76  78\n",
      "                    A          B          C          D\n",
      "2020-12-01   0.000000   2.000000   4.000000   6.000000\n",
      "2020-12-02   6.000000   8.000000  10.000000  12.000000\n",
      "2020-12-03  12.923077  14.923077  16.923077  18.923077\n",
      "2020-12-04  20.400000  22.400000  24.400000  26.400000\n",
      "2020-12-05  28.165289  30.165289  32.165289  34.165289\n",
      "2020-12-06  36.065934  38.065934  40.065934  42.065934\n",
      "2020-12-07  44.025618  46.025618  48.025618  50.025618\n",
      "2020-12-08  52.009756  54.009756  56.009756  58.009756\n",
      "2020-12-09  60.003658  62.003658  64.003658  66.003658\n",
      "2020-12-10  68.001355  70.001355  72.001355  74.001355\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(np.arange(0,80,2).reshape(10,4),\n",
    "   index = pd.date_range('12/1/2020', periods=10),\n",
    "   columns = ['A', 'B', 'C', 'D'])\n",
    "#设置com=0.5，先加权再求均值\n",
    "print(df)\n",
    "print(df.ewm(com=0.5).mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "fd143c96",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.ewm?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb886a6c",
   "metadata": {},
   "source": [
    "# 应用聚合函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "67950329",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             A   B   C   D\n",
      "2020-12-01   0   2   4   6\n",
      "2020-12-02   8  10  12  14\n",
      "2020-12-03  16  18  20  22\n",
      "2020-12-04  24  26  28  30\n",
      "2020-12-05  32  34  36  38\n",
      "               A     B     C     D\n",
      "2020-12-01   0.0   2.0   4.0   6.0\n",
      "2020-12-02   8.0  12.0  16.0  20.0\n",
      "2020-12-03  24.0  28.0  32.0  36.0\n",
      "2020-12-04  40.0  44.0  48.0  52.0\n",
      "2020-12-05  56.0  60.0  64.0  68.0\n",
      "2020-12-01     0.0\n",
      "2020-12-02     8.0\n",
      "2020-12-03    24.0\n",
      "2020-12-04    40.0\n",
      "2020-12-05    56.0\n",
      "Freq: D, Name: A, dtype: float64\n",
      "               A           B      \n",
      "             sum  mean   sum  mean\n",
      "2020-12-01   0.0   0.0   2.0   2.0\n",
      "2020-12-02   8.0   4.0  12.0   6.0\n",
      "2020-12-03  24.0  12.0  28.0  14.0\n",
      "2020-12-04  40.0  20.0  44.0  22.0\n",
      "2020-12-05  56.0  28.0  60.0  30.0\n",
      "               A     B\n",
      "2020-12-01   0.0   2.0\n",
      "2020-12-02   8.0   6.0\n",
      "2020-12-03  24.0  14.0\n",
      "2020-12-04  40.0  22.0\n",
      "2020-12-05  56.0  30.0\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(np.arange(0,40,2).reshape(5,4),\n",
    "   index = pd.date_range('12/1/2020', periods=5),\n",
    "   columns = ['A', 'B', 'C', 'D'])\n",
    "print(df)\n",
    "#窗口大小为3，min_periods 最小观测值为1\n",
    "r = df.rolling(window=2,min_periods=1)\n",
    "#使用 aggregate()聚合操作\n",
    "print(r.aggregate(np.sum))\n",
    "#对 A 列聚合\n",
    "print(r['A'].aggregate(np.sum))\n",
    "#对 A/B 两列聚合\n",
    "print(r['A','B'].aggregate([np.sum,np.mean]))\n",
    "print(r.aggregate({'A': np.sum,'B': np.mean}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "6b1358bf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    False\n",
      "b     True\n",
      "c    False\n",
      "d     True\n",
      "e    False\n",
      "f    False\n",
      "g     True\n",
      "h    False\n",
      "Name: 0, dtype: bool\n",
      "a     True\n",
      "b    False\n",
      "c     True\n",
      "d    False\n",
      "e     True\n",
      "f     True\n",
      "g    False\n",
      "h     True\n",
      "Name: 0, dtype: bool\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(np.random.randn(5, 3), index=['a', 'c', 'e', 'f','h'])\n",
    "df = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])\n",
    "print(df[0].isnull())\n",
    "print(df[0].notnull())\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68a06d09",
   "metadata": {},
   "source": [
    "# 分组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "3e8de425",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    Name  score option_course\n",
      "0   John     82            C#\n",
      "1  Helen     98        Python\n",
      "2   Sona     91          Java\n",
      "3   Ella     87             C\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd \n",
    "import numpy as np \n",
    "data = {'Name': ['John', 'Helen', 'Sona', 'Ella'], \n",
    "   'score': [82, 98, 91, 87], \n",
    "   'option_course': ['C#','Python','Java','C']} \n",
    "df = pd.DataFrame(data)\n",
    "print(df)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b7dbeec",
   "metadata": {},
   "outputs": [],
   "source": [
    "#生成分组groupby对象\n",
    "print(df.groupby('score')['Name'])\n",
    "print('***')\n",
    "#查看分组\n",
    "print(df.groupby('score').groups)\n",
    "print(df.groupby('score').size())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "9806bd49",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{('Ella', 87): [3], ('Helen', 98): [1], ('John', 82): [0], ('Sona', 91): [2]}\n"
     ]
    }
   ],
   "source": [
    "#查看分组\n",
    "print(df.groupby(['Name','score']).groups)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "37049655",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "9b50ab88",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "242\n"
     ]
    }
   ],
   "source": [
    "print(0b11110010)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "28138570",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0b11110010'"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bin(242)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "0c98a54f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "32099"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "0o76543"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "77644162",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'b', 'c']"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str = ' a b c '\n",
    "str.title()\n",
    "str.upper()\n",
    "str.strip()\n",
    "str.replace(' ','')\n",
    "str.split()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "898667f0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['abc']"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list('abc')\n",
    "'abc'.split()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "b08a17c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2845061109936 2845061109936\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 123\n",
    "b=123\n",
    "print(id(a), id(b))\n",
    "id(a)\n",
    "a is b\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "f5a4ae0f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   种类   产地   水果  数量  价格\n",
      "0  水果   朝鲜   橘子   3   2\n",
      "1  水果   中国   苹果   5   5\n",
      "2  水果   缅甸  哈密瓜   5  12\n",
      "3  蔬菜   中国   番茄   3   3\n",
      "4  蔬菜  菲律宾   椰子   2   4\n",
      "5  肉类   韩国   鱼肉  15  18\n",
      "6  肉类   中国   牛肉   9  20\n",
      "***\n",
      "<pandas.core.groupby.generic.DataFrameGroupBy object at 0x000002966F74AD90>\n",
      "          数量         价格\n",
      "0   4.333333   6.333333\n",
      "1   4.333333   6.333333\n",
      "2   4.333333   6.333333\n",
      "3   2.500000   3.500000\n",
      "4   2.500000   3.500000\n",
      "5  12.000000  19.000000\n",
      "6  12.000000  19.000000\n",
      "         数量        价格\n",
      "0 -1.333333 -4.333333\n",
      "1  0.666667 -1.333333\n",
      "2  0.666667  5.666667\n",
      "3  0.500000 -0.500000\n",
      "4 -0.500000  0.500000\n",
      "5  3.000000 -1.000000\n",
      "6 -3.000000  1.000000\n",
      "      种类  产地  水果  数量  价格\n",
      "种类                      \n",
      "水果 0  水果  朝鲜  橘子   3   2\n",
      "肉类 5  肉类  韩国  鱼肉  15  18\n",
      "蔬菜 3  蔬菜  中国  番茄   3   3\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_67540\\991786961.py:13: FutureWarning: Dropping invalid columns in DataFrameGroupBy.transform is deprecated. In a future version, a TypeError will be raised. Before calling .transform, select only columns which should be valid for the function.\n",
      "  print(df.groupby('种类').transform(np.mean))\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_67540\\991786961.py:16: FutureWarning: Dropping invalid columns in DataFrameGroupBy.transform is deprecated. In a future version, a TypeError will be raised. Before calling .transform, select only columns which should be valid for the function.\n",
      "  print(df.groupby('种类').transform(demean))\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame({'种类':['水果','水果','水果','蔬菜','蔬菜','肉类','肉类'],\n",
    "                '产地':['朝鲜','中国','缅甸','中国','菲律宾','韩国','中国'],\n",
    "                '水果':['橘子','苹果','哈密瓜','番茄','椰子','鱼肉','牛肉'],\n",
    "                '数量':[3,5,5,3,2,15,9],\n",
    "                '价格':[2,5,12,3,4,18,20]})\n",
    "#分组求均值，水果、蔬菜、肉类\n",
    "#对可执行计算的数值列求均值\n",
    "print(df)\n",
    "print('***')\n",
    "print(df.groupby('种类'))\n",
    "print(df.groupby('种类').transform(np.mean))\n",
    "#transform()直接应用demean，实现去均值操作\n",
    "demean = lambda arr:arr-arr.mean()\n",
    "print(df.groupby('种类').transform(demean))\n",
    "#自定义函数\n",
    "# 返回分组的前n行数据\n",
    "def get_rows(df,n): \n",
    "     #从1到n行的所有列\n",
    "    return df.iloc[:n,:]\n",
    "#分组后的组名作为行索引\n",
    "print(df.groupby('种类').apply(get_rows,n=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b637c71b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
